package com.itany.shop.controller;

import com.itany.shop.entity.User;
import com.itany.shop.exception.CodeErrorException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 * @date 2023年10月25日 13:59
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/register.user":
                register(request, response);
                break;
            case "/login.user":
                login(request, response);
                break;
            case "/logout.user":
                logout(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>您访问的资源不存在,请检查请求是否正确</h1>");
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        try {
            userService.register(username,password,phone,address);
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (UserExistException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registerMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");

        String image = (String) request.getSession().getAttribute("code");

        try {
            User user = userService.login(username,password,code,image);
            // 登录成功之后,保存用户信息,将信息保存到session中
            request.getSession().setAttribute("user",user);
            // 每个用户登录进来之后分配一个属于他的购物车
            request.getSession().setAttribute("cart",new Cart());
            // 用户成功登录之后,属于登录业务全部结束,将会进入到其他业务中,此时建议使用重定向
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (CodeErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }
    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/findAll.product");
    }
}
